local skel = fk.CreateSkill {
  name = "rmt_hongdang__yundie",
  tags = { Skill.Permanent, Skill.Limited },
}

Fk:loadTranslationTable{
  ["rmt_hongdang__yundie"] = "云叠",
  [":rmt_hongdang__yundie"] = "持恒技，限定技，出牌阶段，你可以获得所有雷【杀】和【闪电】，然后令场上获得"..
  "<a href='rmt__shenren'>“神人再世”</a>光环。",

  ["rmt__shenren"] = "体力值为场上最小或最大的角色回合结束时，其进行一次<a href='rmt__tianjie'>“天劫”</a>，然后加1点体力上限。",
  ["rmt__tianjie"] = "根据手牌中【闪】的数量，对自己造成雷电伤害",
  ["#rmt_hongdang__yundie"] = "云叠：获得所有雷【杀】和【闪电】，场上获得“神人再世”光环！",

  ["@[:]rmt_hongdang__yundie"] = "",
  ["rmt_hongdang__yundie_banner"] = "神人再世",
  [":rmt_hongdang__yundie_banner"] = "体力值为场上最小或最大的角色回合结束时进行一次“天劫”（根据手牌中【闪】的数量对自己造成雷电伤害），"..
  "然后加1点体力上限！",

  ["$rmt_hongdang__yundie1"] = "荡荡元始初，混沌气未分。三色无中化，回合霭庆云。",
  ["$rmt_hongdang__yundie2"] = "飘飖八极，与神人俱。思得神药，万岁为期。",
}

skel:addEffect("active", {
  anim_type = "control",
  prompt = "#rmt_hongdang__yundie",
  card_num = 0,
  target_num = 0,
  can_use = function(self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryGame) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = effect.from
    room:setBanner("@[:]rmt_hongdang__yundie", "rmt_hongdang__yundie_banner")
    local cards = {}
    for _, id in ipairs(room.draw_pile) do
      if table.contains({ "thunder__slash", "lightning" }, Fk:getCardById(id).name) then
        table.insert(cards, id)
      end
    end
    for _, id in ipairs(room.discard_pile) do
      if table.contains({ "thunder__slash", "lightning" }, Fk:getCardById(id).name) then
        table.insert(cards, id)
      end
    end

    for _, p in ipairs(room.alive_players) do
      for _, id in ipairs(p:getCardIds("hej")) do
        if table.contains({ "thunder__slash", "lightning" }, Fk:getCardById(id).name) then
          table.insert(cards, id)
        end
      end
    end
    cards = table.filter(cards, function (id)
      return not table.contains(player:getCardIds("h"), id)
    end)

    if #cards > 0 then
      room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonPrey, skel.name, nil, true, player)
    end
  end,
})

skel:addEffect(fk.TurnEnd, {
  anim_type = "special",
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and player.room:getBanner("@[:]rmt__yundie") and
      (table.every(player.room.alive_players, function (p)
        return p.hp >= player.hp
      end) or
      table.every(player.room.alive_players, function (p)
        return p.hp <= player.hp
      end))
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = math.max(1, #table.filter(player:getCardIds("h"), function(c)
      return Fk:getCardById(c).trueName == "jink"
    end))
    room:damage{
      from = player,
      to = player,
      damage = n,
      damageType = fk.ThunderDamage,
      skillName = "rmt__tianjie",
    }
    if player.dead then return end
    room:changeMaxHp(player, 1)
  end,
})

return skel
